package scottb.flex;

import javax.swing.*;
import java.awt.*;

public class FlexLabel extends JLabel implements FlexLayout {

    public static Font FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 12);
    public static Color COLOR = new Color(0x333333);

    // Layout and position
    private Integer top = null;
    private Integer left = null;
    private Integer bottom = null;
    private Integer right = null;
    private Integer horizontalCenter = null;
    private Integer verticalCenter = null;
    private Integer width = null;
    private Integer height = null;

    public FlexLabel() {

        setFont(FONT);
        setForeground(COLOR);
    }

    public FlexLabel(String text) {
        super(text);

        setFont(FONT);
        setForeground(COLOR);
    }

    // Layout and position
    public FlexLabel top(Integer top) {
        this.top = top;
        return this;
    }

    public Integer top() {
        return top;
    }

    public FlexLabel left(Integer left) {
        this.left = left;
        return this;
    }

    public Integer left() {
        return left;
    }

    public FlexLabel right(Integer right) {
        this.right = right;
        return this;
    }

    public Integer right() {
        return right;
    }

    public FlexLabel bottom(Integer bottom) {
        this.bottom = bottom;
        return this;
    }

    public Integer bottom() {
        return bottom;
    }

    public FlexLabel horizontalCenter(Integer center) {
        this.horizontalCenter = center;
        return this;
    }

    public Integer horizontalCenter() {
        return this.horizontalCenter;
    }

    public FlexLabel verticalCenter(Integer center) {
        this.verticalCenter = center;
        return this;
    }

    public Integer verticalCenter() {
        return this.verticalCenter;
    }

    public FlexLabel center() {
        verticalCenter(0);
        horizontalCenter(0);
        return this;
    }

    public FlexLabel width(Integer width) {
        this.width = width;
        return this;
    }

    public Integer width() {
        return width;
    }

    public FlexLabel height(Integer height) {
        this.height = height;
        return this;
    }

    public Integer height() {
        return height;
    }

    public Dimension getPreferredSize() {

        int width;
        int height;
        Dimension preferredSize = null;

        if(null != width()) {
            width = width();
        }
        else {

            preferredSize = super.getPreferredSize();
            width = preferredSize.width;
        }

        if(null != height()) {
            height = height();
        }
        else {

            if(null == preferredSize) {
                preferredSize = super.getPreferredSize();
            }

            height = preferredSize.height;
        }

        return new Dimension(width, height);
    }
}
